"""Header_Information
------------------------------------
    File_Name: login
    Author: RR9
    Date: 2024/10/16
    Description: 
------------------------------------
"""

from fastapi import APIRouter, HTTPException
from app.models.ob1 import Student
from app.schemas.ob1 import Login
import hashlib

router = APIRouter()


@router.post("/login/", summary="用户登录")
async def login(student: Login):
    pwd = student.password
    print('pwd:', pwd)
    has_pwd = hashlib.md5(pwd.encode()).hexdigest()
    stu_obj_name = await Student.filter(name=student.name).first()
    stu_obj = await Student.filter(name=student.name, pwd=has_pwd).first()
    if not stu_obj_name:
        raise HTTPException(status_code=404, detail="用户名不正确")

    if not stu_obj:
        raise HTTPException(status_code=404, detail="用户密码错误")

    return {"code": 200, "message": f"""欢迎{stu_obj.name}"""}
